package cloudContact.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.mybeans.factory.RollbackException;

import cloudContact.*;
import cloudContact.DAO.*;
import cloudContact.SecurityManager;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Login() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String login = request.getParameter("login");
		String password = request.getParameter("password");

		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();

		try {

			UserDAO userDAO = UserDAO.getInstance();

			User user = userDAO.lookup(login);

			if (user == null) {
				out.print("Error: Invalid user name or password.");

				System.out.println("Login not found : " + login);

				return;
			}

			System.out.println(SecurityManager.getHash(password));
			
			if (SecurityManager.getHash(password).equals(user.getPassword())) {
				// Valid login
				HttpSession session = request.getSession();
				session.setAttribute("authorized", true);
				session.setAttribute("userId", login);

				if (user.getGMailAccessToken() == null
						|| user.getGMailAccessToken().equals("")) {
					out.print("/CloudContact/GoogleOAuth");
				} else {
					out.print("/CloudContact/");
				}

				System.out.println("Login success : " + login);
				System.out.println("Session ID : " + session.getId());
				// out.print(request.getParameter("requestUrl").toString().replaceAll("\\\\",
				// ""));
			} else {
				// Invalid login
				out.print("Error: Invalid user name or password.");

				System.out.println("Incorrect password : " + login + ":"
						+ password);

				return;
			}
		} catch (MyDAOException e) {
			out.print(Common.CriticalErrorRaised(e));
		} catch (RollbackException e) {
			out.print(Common.CriticalErrorRaised(e));
		} catch (NoSuchAlgorithmException e) {
			out.print(Common.CriticalErrorRaised(e));
		} finally {
			out.flush();
			out.close();
		}
	}

}
